(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.acctNoListController", acctNoListController);
	acctNoListController.$inject = [ "$scope", "$filter", "$uibModalInstance", "dialogsModule", "gridService", "layoutConfig", "validatorConfig", "authService", "ajaxServiceFactory" ];
	function acctNoListController($scope, $filter, $uibModalInstance, dialogsModule, gridService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		var ajaxService = ajaxServiceFactory.buildAjaxService({
			settlementInitData : {
				url : "do/cls/dataList/settlementInitData",
				method : "get"
			}
		});

		ctrl.ok = function() {
			if (ctrl.selectedRows && ctrl.selectedRows[0]) {
				ctrl.selectedRows[0].custAcctId = $filter("encode")(ctrl.selectedRows[0].custAcctId);
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		function initParms(payDetail) {
			var custCode = payDetail.companyCode;
			var ccy = payDetail.payCcy;
			var custAcctType = payDetail.custAcctType;
			var payCodeCat = payDetail.payCodeObj.paycodeCat;
			var crDr = payDetail.crDrFlag;

			return {
				"custCode" : custCode ? custCode : "",
				"ccy" : ccy ? ccy : "",
				"custAcctType" : custAcctType ? custAcctType : "",
				"payCodeCat" : payCodeCat ? payCodeCat : "",
				"crDr" : crDr ? crDr : "",
				"custCodeExactFlag" : "Y"
			};
		}

		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = "vm.searchCriteria.list";
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			var payDetail = {};
			if (ctrl.dialogsModule.params.payDetail) {
				payDetail = ctrl.dialogsModule.params.payDetail;
			}
			var parms = {};
			if (ctrl.dialogsModule.params.comeFromId == "mgl") {
				parms = ctrl.dialogsModule.params;
			} else {
				parms = initParms(payDetail);
			}
			ctrl.searchCriteria.custCode = $filter("encode")(parms.custCode);
			ctrl.searchCriteria.custAcctType = parms.custAcctType;
			ctrl.searchCriteria.ccy = parms.ccy;
			ctrl.searchCriteria.paycodeCat = parms.payCodeCat;
			ctrl.searchCriteria.crAndDrflag = parms.crDr;
			ctrl.searchCriteria.custCodeExactFlag = parms.custCodeExactFlag ? parms.custCodeExactFlag : "";// Y : custCode =, else like
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : "";
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : "";
			ctrl.search();
		}
	}
})(angular);
